# encoding: utf-8
from pyo import *


class Grain:
    def __init__(self):
        
        self.table = SndTable('/Users/vincentmonastesse/Documents/alphaOmega/Audio/dIVERS/drift_Call_3.aif')
        self.sndDur = self.table.getDur()

        self.env = HannTable()
        
        self.p1a = SigTo(0.5, time=0.1, init=0.5, mul=self.table.getSize(), add=Noise(5))
        self.p2a = SigTo(value=0.5, time=0.05, init=0.5)
        
        
        
        self.lfo = Sine(self.p1a, mul=.5, add=.5)

        self.z = Sig(0)  # Correspond au parametre 'z' de l'interface tactile

        self.pos = Port(self.p1a, risetime=.05, falltime=.05, mul=self.table.getSize())

        self.dur = Noise(mul=Clip(self.z, min=0, max=0.025), add=.1)

        self.grain = Granulator( table=self.table,    
                            env=self.env,        
                            pitch=self.p2a,      
                            pos=self.p1a,        
                            dur=self.dur,        
                            grains=40,      
                            basedur=.1,      
                            mul=.1)
                            
    def changeTable(self, snd):
        self.table.sound = snd
    
    def changeP1a(self, x):
        self.p1a.value = x

    def changeP2a(self, x):
        self.p2a.value = x
    
    def grainDur(self,x):
        self.z.value = x

    def sig(self):
        return self.grain

class FxSwitch:
    def __init__(self,input,fx='Delay'):
        self.fx = fx
        self.input = input
        
        self.p1b = SigTo(value=0.5, time=0.05, init=0.5)
        self.p2b = SigTo(value=0.5, time=0.05, init=0.5)
        self.delay = Delay(self.input.out(), maxdelay=10, delay=self.p1b*4, feedback=self.p2b).stop()
        self.disto = Disto(self.input, drive=self.p1b, slope=self.p2b, 
                           mul=.5).stop()
        self.degrade = Degrade(self.input, bitdepth=Sig(self.p1b,mul=20,add=2),
                               srscale=Sig(self.p2b,mul=.99,add=.01),
                               mul=1.5).stop()
        self.reverb = WGVerb(self.input.out(), feedback=self.p1b, 
                             cutoff=Sig(self.p2b,mul=15000,add=250)).stop()
        self.harmo = Harmonizer(self.input.out(), 
                                transpo=Sig(self.p1b,mul=24,add=-12),
                                feedback=self.p2b).stop()
        self.fx_dict = {'Reverb': self.reverb, 'Disto': self.disto, 
                        'Degrade': self.degrade, 'Delay': self.delay, 
                        'Harmonizer': self.harmo}
        self.fx_dict[fx].out()
        
    def changeFx(self, fx):
        self.fx_dict[self.fx].stop()
        self.fx_dict[fx].out()
        self.fx = fx
        
    def changeP1b(self, x):
        self.p1b.value = x

    def changeP2b(self, x):
        self.p2b.value = x
